﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace linkouqs.Gamekit {

    public class CharacterStateRun : CharacterState {

        float m_Speed;

        CharacterAbilityDashAttack m_DashAttack;
        CharacterAbilityTake m_Take;
        CharacterAbilityAttack m_Attack;
        CharacterAbilityPush m_Push;

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
            base.OnStateEnter(animator, stateInfo, layerIndex);
            m_Speed = animator.speed;
            m_DashAttack = animator.GetComponent<CharacterAbilityDashAttack>();
            m_Take = animator.GetComponent<CharacterAbilityTake>();
            m_Attack = animator.GetComponent<CharacterAbilityAttack>();
            m_Push = animator.GetComponent<CharacterAbilityPush>();
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
            animator.speed = m_Speed * Mathf.Abs(animator.GetFloat("Horizontal"));

            if (Character.IsPressedRun() && m_Push.CanStartAction()) {
                Character.LockDirection = true;
                m_Push.StartAction();
                return;
            }

            if (!Character.IsPressedAction()) {
                return;
            }

            if (m_Take.CanStartAction()) {
                m_Take.StartAction();
                return;
            }

            if (m_DashAttack.CanStartAction()) {
                m_DashAttack.StartAction();
                return;
            }

            if (m_Attack.CanStartAction()) {
                m_Attack.StartAction();
                return;
            }

        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
            animator.speed = m_Speed;
            m_Push = null;
            m_Attack = null;
            m_Take = null;
            m_DashAttack = null;
            base.OnStateExit(animator, stateInfo, layerIndex);
        }

    }
}